package redisdb

import (
	"github.com/go-redis/redis"
	"time"
)

func (d *RedisDb) DbGet() *redis.Client {
	return d.Db
}

// Set 设置缓存
// k 键
// v 值
// t 过期时间,零值表示不过期
func (d *RedisDb) Set(k string, v interface{}, t ...time.Duration) error {
	var tick time.Duration
	if t != nil {
		tick = t[0]
	}
	return d.Db.Set(k, v, tick).Err()
}

func (d *RedisDb) Get(k string) (string, error) {
	return d.Db.Get(k).Result()
}

func (d *RedisDb) Del(ks ...string) (int64, error) {
	return d.Db.Del(ks...).Result()
}

func (d *RedisDb) Exists(ks ...string) (int64, error) {
	return d.Db.Exists(ks...).Result()
}

func (d *RedisDb) HExists(k, f string) (bool, error) {
	return d.Db.HExists(k, f).Result()
}

func (d *RedisDb) HSet(k, f string, v interface{}) bool {
	return d.Db.HSet(k, f, v).Val()
}

func (d *RedisDb) HGet(k, f string) (string, error) {
	return d.Db.HGet(k, f).Result()
}

func (d *RedisDb) HDel(k string, fs ...string) (int64, error) {
	return d.Db.HDel(k, fs...).Result()
}

func (d *RedisDb) CloseDb() error {
	return d.Db.Close()
}

func (d *RedisDb) HGetAll(key string) (map[string]string, error) {
	return d.Db.HGetAll(key).Result()
}

func (d *RedisDb) Keys(pattern string) ([]string, error) {
	return d.Db.Keys(pattern).Result()
}

func (d *RedisDb) Expire(pattern string, expiration time.Duration) (bool, error) {
	return d.Db.Expire(pattern, expiration).Result()
}
